package bmatch.tech;

import bmatch.tech.model.UploadFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class ShowServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setStatus(200);
        resp.setContentType("text/html");
        PrintWriter writer = resp.getWriter();
        writer.println("<table border=\"1\">");
        writer.println(
                "    <tr>\n" +
                "        <th>文件名</th>\n" +
                "        <th>文件类型</th>\n" +
                "        <th>下载</th>\n" +
                "    </tr>");

        for (int i = 0; i < UploadServlet.getCaches().size(); i++) {
            UploadFile uploadFile = UploadServlet.getCaches().get(i);
            writer.println("    <tr>");
            writer.print("    <td>");
            writer.print(uploadFile.getName());
            writer.println("    </td>");
            writer.print("    <td>");
            writer.print(uploadFile.getMimeType());
            writer.println("    </td>");
            writer.print("    <td>");
            writer.print("<a");
            writer.print(" href=\"/download?fileName="+uploadFile.getStoreFileName()+"\">");
            writer.print("下载</a>");
            writer.println("</td>");
            writer.println("    </tr>");
        }

        writer.println("</table>");

        writer.println(
                "<form action=\"upload\" method=\"post\"  enctype=\"multipart/form-data\">\n " +
                "    <input type=\"file\" name=\"myfile\">\n" +
                "    <input type=\"submit\" value=\"上传文件\">\n" +
                "</form>\n");
    }
}
